package com.gameShare.utils

import java.time.LocalDateTime
import java.time.Period
import java.time.temporal.ChronoUnit

class DateFormatter {

    companion object{

        /**
         * 计算当前时间与加入时间之间的差值，并根据差值大小选择合适的单位。
         * @param joinTime 加入时间
         * @return 差值字符串（例如 "1天", "2个月", "3年"）
         */
        fun computeNowBetweenJoinTime(joinTime: LocalDateTime): String {
            val now = LocalDateTime.now()
            val period = Period.between(joinTime.toLocalDate(), now.toLocalDate())

            return when {
                period.years > 0 -> "${period.years}年前"
                period.months > 0 -> "${period.months}个月前"
                period.days > 0 -> "${period.days}天前"
                ChronoUnit.HOURS.between(joinTime, now) > 0 -> "${ChronoUnit.HOURS.between(joinTime, now)}小时前"
                else -> "${ChronoUnit.MINUTES.between(joinTime, now)}分钟前"
            }
        }

        @JvmStatic
        fun main(args: Array<String>) {
            println(computeNowBetweenJoinTime(LocalDateTime.of(2025, 6, 23, 3, 3)))
        }
    }
}